package com.cd2cd.code_template.configuration.handler;
import com.cd2cd.code_template.util.CommUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.cd2cd.code_template.configuration.constants.ServiceCode.TOKEN_INVALID;

/**
 * 认证失败处理类
 */
@Slf4j
@Component
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        log.error("{}", TOKEN_INVALID);
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        CommUtils.returnCode(response, TOKEN_INVALID);
    }

}